import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/lock/associating_rooms/index.dart";
import "package:ct_pa_majordomo/widgets/button/tap_container.dart";
import "package:ct_pa_majordomo/widgets/divider/solid_divider.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:ct_pa_majordomo/pages/device/entrance_guard/api.dart";

class EntranceGuardInfo extends StatefulWidget {
  final Map info;
  const EntranceGuardInfo({super.key, required this.info});

  @override
  State<StatefulWidget> createState() => _EntranceGuardInfoState();
}

class _EntranceGuardInfoState extends State<EntranceGuardInfo> {

  String _getRoomCount() {
    if (widget.info["roomCount"] == 0) {
      return "${widget.info["roomCount"]}间";
    }
    return "去关联";
  }

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(12),
        boxShadow: const [ThemeVars.boxShadow],
        color: Colors.white,
      ),
      child: Padding(
        padding: const EdgeInsets.all(12),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(widget.info["faceLockName"] ?? "未命名设备",
                  style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600,)),
              ],
            ),
            const SolidDivider(padding: EdgeInsets.symmetric(vertical: 16), color: ThemeVars.colorBorder,),
            Row(
              children: [
                const Text("公区锁："),
                TapContainer(
                    onTap: () {
                      Navigator.push(
                        context,
                        CupertinoPageRoute(
                          builder: (BuildContext context) {
                            return AssociatingRooms(
                              type: LockType.public, 
                              lockDeviceId: widget.info["faceLockId"],
                            );
                          },
                        ),
                      );
                    },
                    child: Text(_getRoomCount(), style: TextStyle(color: ThemeVars.colorPrimary),)
                ),
              ],
            ),
            const SizedBox(height: 8,),
            Row(
              children: [
                const Text("门禁状态："),
                Text(
                  widget.info["statusName"] ?? "未知",
                  style: TextStyle(
                    color: widget.info["statusName"] == "在线" 
                      ? ThemeVars.colorPrimary
                      : ThemeVars.colorError
                  ),
                ),
              ],
            ),
            const SizedBox(height: 8,),
            Row(
              children: [
                const Text("更新时间："),
                Text(widget.info["lastLoginTime"] ?? "-"),
              ],
            )
          ],
        ),
      ),
    );
  }
}